package easy.两数之和;

import java.util.HashMap;
import java.util.Map;

public class TwoSum {

    public static int[] myAnswer(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int temp = target - nums[i];
            if (map.containsKey(temp)) {
                return new int[]{map.get(temp), i};
            }
            map.put(nums[i], i);
        }

        throw new IllegalArgumentException("无结果");
    }

    public static int[] otherAnswer(int[] nums, int target) {
        int volume = 2048; //100000000000
        int bitMode = volume - 1;//011111111111
        int[] result = new int[volume];

        for (int i = 0; i < nums.length; i++) {
            int c = (target - nums[i]) & bitMode;
            if (result[c] != 0) {
                return new int[]{result[c] - 1, i};
            }
            result[nums[i] & bitMode] = i + 1;
        }
        return null;
    }
}
